<!-- 正文开始 -->
<div class="layui-fluid" id="appVersion">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar" lay-filter="appVersion">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">类型</label>
                        <div class="layui-input-inline">
                            <select name="softType" lay-verify="required">
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">版本号</label>
                        <div class="layui-input-block">
                            <input name="fullVersion" class="layui-input" type="text" placeholder="请输版本号" autocomplete="off" lay-verify="title">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-upload">
                            <button class="layui-btn layui-btn-normal" data-id="btnChooseFile" type="button">选择文件</button>
                            <button class="layui-btn" data-id="btnDoUpload" type="button">开始上传</button>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">描述</label>
                    <div class="layui-input-block">
                        <textarea name="remark" class="layui-textarea" placeholder="" autocomplete="off"></textarea>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="appVersionTable" lay-filter="appVersionTable"></table>

        </div>
    </div>
    <!-- 表格操作列 -->
    <script type="text/html" class="tpl-appVersionTable-operator">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <!-- 表格状态列 -->
    <script type="text/html" class="tpl-appVersionTable-status">
        <input type="checkbox" lay-filter="tpl-appVersionTable-status" value="{{d.id}}" lay-skin="switch" lay-text="有效|无效"
               {{d.status==1?'checked':''}}/>
    </script>

</div>


<script>
    layui.use(['layer', 'table', 'form', 'http','upload','tools'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        var http = layui.http;
        var tools = layui.tools;
        var upload = layui.upload;

        var viewId = "#appVersion";
        var $view = $(viewId);

        tools.select.codes('AppType',$view.find("select[name='softType']"),function (res) {
            if(res.ok){
                form.render('select','appVersion');
            }
        });

        // 渲染表格
        var appVersionTableIns = table.render({
            elem: '#appVersionTable',
            url: http.base_server + 'appVersion/list',
            method: 'GET',
            where: {
                // access_token: http.getToken().access_token
            },
            page: false,
            cellMinWidth: 50,
            hide: true,
            height: 'full-290',
            cols: [[
                {field: 'id', title: 'ID',width:50, hide:true},
                {field: 'softTypeName', sort: true, title: '类型',width:150},
                {field: 'softName', sort: false, title: '文件名',width:150},
                {field: 'fullVersion', sort: true, title: '版本号',width:100},
                {field: 'folderUrl', sort: false, title: '文件路径', width:200},
                {field: 'status', sort: false, title: '状态', templet: $view.find('.tpl-appVersionTable-status'),width:100},
                {field: 'remark', sort: false, title: '描述',width:200},
                {align: 'center', title: '操作', width: 80, toolbar: $view.find('.tpl-appVersionTable-operator')},
                {field: 'updateTime', sort: false, title: '更新时间'},
                {field: 'updateByWho', sort: false, title: '更新人'}
            ]],
            error: function(code,msg){
                http.fail(code,msg);
            }
        });

        // 修改appVersion状态
        form.on('switch(tpl-appVersionTable-status)', function (obj) {
            http.post('appVersion/update', {
                id: obj.elem.value,
                status: obj.elem.checked ? 1 : 0
            }, function (res) {
                if (res.ok) {
                    layer.msg(res.msg, {icon: 1});
                    // table.reload('table-user', {});
                } else {
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            });
        });

        //选完文件后不自动上传
        upload.render({
            elem: $view.find("button[data-id='btnChooseFile']")
            ,url: http.base_server + 'appVersion/upload/'
            ,auto: false
            ,accept: 'file' //普通文件
            //,multiple: true
            ,bindAction: $view.find("button[data-id='btnDoUpload']")
            ,data: function(){ return tools.formVal(viewId);} //必须是函数
            ,exts: 'apk|txt'
            ,before: function(obj){
                var json = tools.formVal(viewId);
                //console.log(json);
                if(!json){
                    http.failMsg("获取表单数据失败");
                    return false;
                }

                if(tools.isEmpty(json.softType)) {
                    http.failMsg("请输入类型");
                    return false;
                }
                if(tools.isEmpty(json.fullVersion)) {
                    http.failMsg("请输入版本号");
                    return false;
                }
                if(!$.isNumeric(json.fullVersion)){
                    http.failMsg("版本号应该是数字");
                    return false;
                }
                if(tools.isEmpty(json.remark)) {
                    http.failMsg("请输入描述");
                    return false;
                }

                layer.load(2); //上传loading
                return true;
            }
            ,done: function(res){
                layer.closeAll('loading'); //关闭loading
                if(http.ok(res.code)) {
                    appVersionTableIns.reload();
                    http.okMsg("上传成功");
                    form.val('appVersion',{
                        'fullVersion':'',
                        'remark':''
                    })
                }else{
                    http.fail(res);
                }
            },
            error:function (index,upload,status,msg) {
                layer.closeAll('loading'); //关闭loading
                http.fail(status,msg);
            }
        });

        table.on('tool(appVersionTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') { // 删除
                layer.confirm("确定删除该行?",{btn:["确定","取消"]},
                    function(index,obj) {
                        layer.closeAll('dialog');
                        http.get('appVersion/delete', {id: data.id}, function (res) {
                            if (res.ok) {
                                appVersionTableIns.reload();
                            }
                        })
                    }
                );
            }
        });
    });
</script>